require(['jquery' , 'underscore','md5','alertify','validate'],function(jq,_,md5,alertify) {
 
	
	jq('#user_profile').on('click', function (e) {
		if(jq("#formUserProfile").length==0){ 
			jq(document.body).append(jq("#templateUserProfile").text());
		}
		jq('#formUserProfile').modal('show'); 
		jq("#formUserProfile form").load(CTX+"/user/detail"); 
		jq('#formUserProfile').one('hide.bs.modal', function (e) {
			//删除此DOM结点，防止内容的DOM与此DOM下的元素冲突
			jq('#formUserProfile').remove(); 
		});  

		jq("#formUserProfile button.user-profile-save").one("click",function(){    
			var pass = jq("#formUserProfile #password").val();
			pass &&	jq("#formUserProfile #password").val(md5.hex_md5(pass));
			jq.post(CTX+"/user/save", jq("#formUserProfile form").serialize() ,function(data){ 
				console.log(data);
				jq('#formUserProfile').modal('hide'); 
			});
		});
	}) 
	
	
	jq('#dialogUserChanagePassword').on('show.bs.modal', function (e) { 
		jq("#dialogUserChanagePassword form").load(CTX+"/user/changepassword"); 
	});
	
	jq('#dialogUserChanagePassword button.user-chanage-password-save').on('click', function (e) {
		if(! $("#dialogUserChanagePassword form").valid()){
			return ;
		}
		
		jq("#cp_oldpassword").val(md5.hex_md5(jq("#cp_oldpassword").val()));
		jq("#cp_new_password").val(md5.hex_md5(jq("#cp_new_password").val()));
		jq("#cp_db_password").val(md5.hex_md5(jq("#cp_db_password").val()));  
		jq.post(CTX+"/user/savepassword", jq("#dialogUserChanagePassword form").serialize() ,function(data){ 
				console.log(data);
				if(data.state!=0){
					alertify.error(data.message);
					$("#dialogUserChanagePassword form")[0].reset();
				} 
				else{
					alertify.success("密码修改成功");
					jq('#dialogUserChanagePassword').modal('hide'); 
				}
		}); 
	}) 
	
	$("#dialogUserChanagePassword form").validate({
        rules: {  
        	new_db_password: {
			    required: true, 
			    equalTo: "#cp_new_password"
			   } 
		  }
    });  
});